package com.zcxy360.sandtable.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcxy360.sandtable.domain.CourseStudentMapper;
import com.zcxy360.sandtable.domain.CourseTeam;
import com.zcxy360.sandtable.domain.CourseTeamMapper;
import com.zcxy360.sandtable.domain.TeamDrawingCommit;
import com.zcxy360.sandtable.domain.TeamDrawingCommitMapper;
import com.zcxy360.sandtable.util.ShiroUtil;
import com.zcxy360.sandtable.util.StringUtil;
import com.zcxy360.sandtable.util.UploadUtil;

@Transactional
@Service
public class CourseTeamService {

	@Value("${res.server}")
	private String resServer;
	
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private CourseTeamMapper courseTeamMapper;
	@Autowired
	private CourseStudentMapper courseStudentMapper;
	@Autowired
	private TeamDrawingCommitMapper teamDrawingCommitMapper;

	public CourseTeam getByTeamId(Integer teamId) {
		return courseTeamMapper.selectByPrimaryKeyWithCascade(teamId);
	}

	public Object list(CourseTeam courseTeam, String key, String orderByClause, Integer pageNum, Integer pageSize,
			Integer navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		List<CourseTeam> courseTeams = courseTeamMapper.listWithCascade(courseTeam,StringUtil.wrapper(key),orderByClause);
		return new PageInfo<>(courseTeams, navigatePages);
	}

	public CourseTeam post(CourseTeam courseTeam) {
		courseTeamMapper.insertSelective(courseTeam);
		return courseTeam;
	}

	public boolean update(CourseTeam courseTeam) {
		courseTeamMapper.updateByPrimaryKeySelective(courseTeam);
		return true;
	}

	public boolean delete(int[] teamIds) {
		courseTeamMapper.deleteBatch(teamIds);
		return true;
	}

	public boolean inferSubmit(CourseTeam courseTeam, String drawingJson, MultipartFile file) {
		String teamRole = courseStudentMapper.selectTeamRole(courseTeam.getTeamId(),ShiroUtil.getUser().getUserId());
		if(!teamRole.equals("ceo")) {
			throw new RuntimeException("只有ceo可以操作");
		}
		
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, TeamDrawingCommit.class);
		List<TeamDrawingCommit> teamDrawingCommits = null;
		try {
			teamDrawingCommits = objectMapper.readValue(drawingJson, javaType);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("解析数据失败");
		} 
		teamDrawingCommitMapper.updateBatch(teamDrawingCommits);
		
		if(file != null) {
			String path = UploadUtil.uploadVideo(file);
			courseTeam.setInferVideoPath(path.replace("E:/res", resServer));
		}
		courseTeam.setIsInferCommitted(true);
		courseTeam.setInferCommitTime(new Date());
		courseTeamMapper.updateByPrimaryKeySelective(courseTeam);
		courseStudentMapper.updateTeamCompletion(courseTeam.getTeamId(),true);
		return true;
	}

	public boolean roadSubmit(CourseTeam courseTeam, MultipartFile pdfFile, MultipartFile videoFile) {
		String teamRole = courseStudentMapper.selectTeamRole(courseTeam.getTeamId(),ShiroUtil.getUser().getUserId());
		if(!teamRole.equals("ceo")) {
			throw new RuntimeException("只有ceo可以操作");
		}
		
		if(pdfFile != null) {
			String path = UploadUtil.uploadPdf(pdfFile);
			courseTeam.setRoadPdfPath(path.replace("E:/res", resServer));
		}
		if(videoFile != null) {
			String path = UploadUtil.uploadVideo(videoFile);
			courseTeam.setRoadVideoPath(path.replace("E:/res", resServer));
		}
		courseTeam.setIsRoadCommitted(true);
		courseTeam.setRoadCommitTime(new Date());
		courseTeamMapper.updateByPrimaryKeySelective(courseTeam);
		courseStudentMapper.updateTeamCompletion(courseTeam.getTeamId(),true);
		return true;
	}

	public boolean mark(CourseTeam courseTeam) {
		if(courseTeam.getInferScore() != null) {
			courseTeam.setIsInferPublished(true);
			courseTeam.setInferPublishTime(new Date());
		}
		if(courseTeam.getRoadScore() != null) {
			courseTeam.setIsRoadPublished(true);
			courseTeam.setRoadPublishTime(new Date());
		}
		courseTeamMapper.updateByPrimaryKeySelective(courseTeam);
		return true;
	}

	public Object back(CourseTeam courseTeam) {
		courseTeamMapper.updateByPrimaryKeySelective(courseTeam);
		courseStudentMapper.updateTeamCompletion(courseTeam.getTeamId(),false);
		return true;
	}
	
}
